package com.boot.admin.modules.sys.ai.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.admin.base.R;
import com.boot.admin.core.service.BaseServiceImpl;
import com.boot.admin.data.domain.BasePage;
import com.boot.admin.modules.sys.ai.mapper.AiRoleMapper;
import com.boot.admin.modules.sys.ai.model.entity.AiRole;
import com.boot.admin.modules.sys.ai.model.query.AiRoleQuery;
import com.boot.admin.modules.sys.ai.model.vo.AiRoleVO;
import com.boot.admin.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AI角色服务
 *
 * @author 李飞洋
 * @date 2025/9/24
 */
@Slf4j
@Service
public class AiRoleService extends BaseServiceImpl<AiRoleMapper, AiRole> {

    /**
     * 分页查询
     */
    public BasePage<AiRoleVO> queryPage(AiRoleQuery query) {
        Page<AiRoleVO> page = new Page<>(query.getCurrent(), query.getSize());
        List<AiRoleVO> vos = this.baseMapper.queryPage(page, query);
        return new BasePage<>(page.getCurrent(), page.getSize(), page.getTotal(), vos);
    }

    /**
     * 列表查询
     */
    public List<AiRoleVO> queryList(AiRoleQuery query) {
        return this.baseMapper.queryPage(null, query);
    }

    /**
     * 查询启用的角色
     */
    public List<AiRoleVO> queryEnabledRoles() {
        AiRoleQuery query = new AiRoleQuery();
        query.setIsEnabled(1);
        return this.baseMapper.queryPage(null, query);
    }

    /**
     * 根据ID获取详情
     */
    public R<AiRoleVO> detail(String id) {
        AiRole role = getById(id);
        if (null == role) {
            return R.NG("角色不存在");
        }
        AiRoleVO res = BeanUtil.toBean(role, AiRoleVO.class);
        return R.OK(res);
    }

    /**
     * 保存角色
     */
    @Transactional(rollbackFor = Exception.class)
    public R<AiRoleVO> save(AiRoleVO vo) {
        if (StrUtil.isBlank(vo.getRoleName())) {
            return R.NG("角色名称不能为空");
        }
        if (StrUtil.isBlank(vo.getSystemPrompt())) {
            return R.NG("系统提示词不能为空");
        }
        if (StrUtil.isBlank(vo.getVoiceId())) {
            return R.NG("语音ID不能为空");
        }
        
        AiRole role = BeanUtil.toBean(vo, AiRole.class);
        role.setId(IdUtil.simpleUUID());
        role.setCreated(LocalDateTime.now());
        role.setCreatedBy(SecurityUtil.getCurrentUsername());
        role.setModified(LocalDateTime.now());
        role.setModifiedBy(SecurityUtil.getCurrentUsername());
        
        if (null == role.getIsEnabled()) {
            role.setIsEnabled(1);
        }
        if (null == role.getSortOrder()) {
            role.setSortOrder(0);
        }
        
        save(role);
        return R.OK(vo);
    }

    /**
     * 更新角色
     */
    @Transactional(rollbackFor = Exception.class)
    public R<AiRoleVO> updateById(String id, AiRoleVO vo) {
        AiRole role = getById(id);
        if (null == role) {
            return R.NG("角色不存在");
        }
        
        BeanUtil.copyProperties(vo, role);
        role.setModified(LocalDateTime.now());
        role.setModifiedBy(SecurityUtil.getCurrentUsername());
        
        updateById(role);
        return R.OK(vo);
    }

    /**
     * 批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    public R<String> deleteByIds(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return R.NG("参数不能为空");
        }
        removeByIds(ids);
        return R.OK();
    }

    /**
     * 启用/禁用角色
     */
    @Transactional(rollbackFor = Exception.class)
    public R<String> enableRole(String id, boolean enable) {
        AiRole role = getById(id);
        if (null == role) {
            return R.NG("角色不存在");
        }
        
        role.setIsEnabled(enable ? 1 : 0);
        role.setModified(LocalDateTime.now());
        role.setModifiedBy(SecurityUtil.getCurrentUsername());
        
        updateById(role);
        return R.OK();
    }

    /**
     * 根据ID获取角色信息
     */
    public AiRole getRoleById(String roleId) {
        return getById(roleId);
    }
}